package com.kma.summer2012.client.view.requests;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.constants.ConstantsHolder;
import com.kma.summer2012.client.styles.style2.Recources;

import java.util.Date;

public class
        CreateRequest extends Request {

    @UiTemplate("Request.ui.xml")
    interface CreateRequestUiBinder extends UiBinder<HTMLPanel, Request> {
    }

    private static CreateRequestUiBinder createRequestUibinder = GWT.create(CreateRequestUiBinder.class);

    public CreateRequest() {

        super(createRequestUibinder);

        title.setText(constants.newRequest());

        this.state.setText("New");
        DateTimeFormat fmt = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT());
        Date date = new Date();
        this.datelbl.setText(fmt.format(date));

        hideField();

        this.catalog.setEnabled(true);
        this.catalog.setVisible(true);
        this.totalSumOfWork = 0;

        this.datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
            public void onValueChange(ValueChangeEvent<Date> event) {
                Date date = event.getValue();
                String dateString = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_SHORT()).format(date);
                text.setText(dateString);
            }
        });
        if (!(flexTable.getRowCount() > 0)) {
            flexTable.setVisible(false);
        }

        addStartToRequiredField();
        setInnerStyle();
    }

    public void hideField() {
        this.comments.setVisible(false);
        this.labelComments.setText("");
        this.sendForProcessingButton.setEnabled(false);
        this.editButtonManager.setVisible(false);
        this.editButtonOperator.setVisible(false);
        this.id.setVisible(false);
        this.labelID.setText("");

    }

    @Override
    public Widget asWidget() {
        return this;
    }

    private void setInnerStyle() {
        //Input field that could be edited
        requestInfo.addStyleName(Recources.INSTANCE.styles().TextAreaWritable());
        clAddress.addStyleName(Recources.INSTANCE.styles().TextAreaWritable());
        clName.addStyleName(Recources.INSTANCE.styles().TextAreaWritable());
        clMail.addStyleName(Recources.INSTANCE.styles().TextAreaWritable());

    }

    private void addStartToRequiredField() {
        this.labelTypeOfWork.setText(constants.typeOfWork().concat("*"));
        this.labelTimeRequired.setText(constants.timeRequired().concat("*"));
        this.labelDateAndTime.setText(constants.reqDate_Time().concat("*"));
        this.labelClName.setText(constants.clName().concat("*"));
        this.labelClMail.setText(constants.clEmail().concat("*"));
        this.labelClAddress.setText(constants.clAdd().concat("*"));

    }

}
